<?php

namespace Biz\service\advertisement\apiModule;


use Doraemon\model\Position\PositionKa;
use Doraemon\service\statSign\Basic as statSign;
use Doraemon\tools\ImgUrl;

class SearchResultCard extends BaseModule
{

    const FORMAT_HIT = 'formatPosHitAll';

    public function extCheckActive($v, $extInput)
    {
        $key_word = $extInput['key_word'] ?? '';
        $config = $v['ext']['key_word'] ?? '';
        $config = explode(',', $config);
        return strlen($key_word) && in_array($key_word, $config);
    }

    public function format($hit, $uid, $userProfileExt, $statSignExt)
    {

        $response = [];
        if (empty($hit)) return $response;

        foreach ($hit as $pos => $value) {
            if ($value) {
                foreach ($value as $v) {
                    $ext = $v['ext'] ?? [];
                    $temp = [
                        'id' => $v['id'],
                        'pos' => $v['pos'],
                        'link' => $this->generateLink($v['link_type'], $v['link_params']),
                        'key_word' => $ext['key_word'] ?? '',
                        'pic_url' => strlen($v['pic_id']) ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $v['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI) : '',
                        'single_pic_url' => strlen($v['single_pic_id']) ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $v['single_pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI) : '',
                        'material_id' => $ext['material_id'] ?? '',
                        'statSign' => $this->statSign($v, [statSign::EXTEND_KEY_OBJ_ID => $ext['material_id'] ?? '']),
                    ];
                    $response[$pos][] = $temp;
                }
            }
        }
        return $response;
    }

    public function extListMap($ids)
    {
        $list = empty($ids) ? [] : PositionKa::getListByPositionIds($ids);
        return array_column($list, null, 'position_id');
    }

    public function getExtInfo($baseInfo)
    {
        $id = $baseInfo['id'] ?? 0;
        $info = empty($id) ? [] : PositionKa::getListByPositionIds($id);
        $info = empty($info) ? [] : current($info);
        $info['type'] = empty($baseInfo['pic_id']) && empty($baseInfo['single_pic_id']) ? 1 : 2;
        return $info;
    }

    public function uniquePos($v): string
    {
        $pos = $v['pos'];
        $key_word = $v['ext']['key_word'] ?? '';
        $unique = $pos . "_" . $key_word;
        return $unique;
    }
}